namespace AgentJohnson
{
  using System;
  using System.Linq;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Feature.Services.CSharp.Bulbs;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.Statements.Blocks;
  using Resharper.Abstractions.CodeDom.TypeMembers;
  using Resharper.Abstractions.CodeDom.Types.Classes;

  /// <summary>Defines the <see cref="MakeVirtual"/> class.</summary>
  [ContextAction("Make abstract class virtual [Agent Johnson]", "Converts an abstract class to a normal class with virtual members.")]
  public class MakeVirtual : ContextActionBase
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="MakeVirtual"/> class.</summary>
    /// <param name="provider">The provider.</param>
    public MakeVirtual([NotNull] ICSharpContextActionDataProvider provider) : base(provider)
    {
    }

    #endregion

    #region Methods

    /// <summary>Determines whether this instance can execute the specified action context.</summary>
    /// <param name="context">The action context.</param>
    /// <returns><c>true</c> if this instance can execute the specified action context; otherwise, <c>false</c>.</returns>
    protected override bool CanExecute(CanExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      return this.GetModel(context) != null;
    }

    /// <summary>Executes the action.</summary>
    /// <param name="context">The execute context.</param>
    protected override void Execute(ExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var model = this.GetModel(context);
      if (model == null)
      {
        return;
      }

      model.Class.IsAbstract = false;

      foreach (var function in model.Class.Functions.Where(f => f.IsAbstract))
      {
        function.IsAbstract = false;
        function.IsVirtual = true;
        if (function.Body == null)
        {
          function.Body = context.Factory.CreateEmptyBlock();
        }
      }

      foreach (var property in model.Class.Properties.Where(p => p.IsAbstract))
      {
        property.IsAbstract = false;
        property.IsVirtual = true;

        foreach (var accessor in property.Accessors.Where(a => a.Body == null))
        {
          accessor.Body = context.Factory.CreateEmptyBlock();
        }
      }
    }

    /// <summary>Gets the text.</summary>
    /// <returns>Returns the text.</returns>
    protected override string GetText()
    {
      return "Make virtual [Agent Johnson]";
    }

    /// <summary>Gets the model.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the model.</returns>
    private Model GetModel(ProviderContext context)
    {
      var text = context.GetSelectedText();
      if (text != "abstract")
      {
        return null;
      }

      var @class = context.GetContainingClass();
      if (@class == null)
      {
        return null;
      }

      var typeMember = context.GetSelectedTypeMember();
      if (typeMember != null && typeMember.Name != @class.Name)
      {
        return null;
      }

      return new Model
      {
        Class = @class
      };
    }

    #endregion

    /// <summary>Defines the <see cref="Model"/> class.</summary>
    private class Model
    {
      #region Properties

      /// <summary>
      /// Gets or sets the class.
      /// </summary>
      /// <value>The class.</value>
      [NotNull]
      public Class Class { get; set; }
      #endregion
    }
  }
}